/**
 * 创建于：2016-5-11
 * 创建人：杨骐彰
 * 说明： 主页面
 */

import React from 'react'
import NavBar from './navbar'
import SideBar from './sidebar'
import MainBody from './mainbody'
import auth from 'auth'
import store from 'store'
import {sidebar} from 'icons'
import classnames from 'classnames'
import {getParentNode} from 'utils'
import BaseComponent from 'basecomponent'
import apis from 'apis'
import {Affix} from 'antd'
import './style.scss'

const blocksSuper =[
  {
    id:1,
    title:`推\u00A0\u00A0荐`,
    isShowSecondaryMenu: false,
    isMenu:true,
    list:[{
      title:'推荐学校',
      href:'/home/recommend/company',
    },{
      title:'推荐精品',
      href:'/home/recommend/job',
      subHerf:['/home/recommend/job/revise',
        '/home/recommend/job/revise/major',
      ]
    }]
  },
  {
    id:2,
    title:'分\u00A0\u00A0类',
    isShowSecondaryMenu: false,
    isMenu:true,
    list: [{
      title:'学校大全',
      href: '/home/class/company'
    },{
      title:'专业大全',
      href: '/home/class/job',
      subHerf:[
        '/home/class/job/addMajor'
      ]
    }]
  },
  {
    id:3,
    title:'就\u00A0\u00A0业',
    isShowSecondaryMenu: false,
    isMenu:true,
    list: [{
      href: '/home/employment'
    }]
  },
  {
    id:4,
    title:'校\u00A0&\u00A0企',
    isShowSecondaryMenu: false,
    isMenu:true,
    list: [{
      title: '学校',
      href: '/home/adv/school'
    },{
      title: '企业',
      href: '/home/adv/company'
    },{
      title: '资本',
      href: '/home/adv/capital'
    }]
  },
  {
    id:5,
    title:'横\u00A0\u00A0幅',
    isShowSecondaryMenu: false,
    isMenu:false,
    list: [{
      href: '/home/banner'
    }]
  },
  {
    id:6,
    title:'纷\u00A0\u00A0声',
    isShowSecondaryMenu: false,
    isMenu:false,
    list: [{
      href: '/home/fensheng'
    }]
  }
];
const blocksManager = [
  {
    id:1,
    title:`推\u00A0\u00A0荐`,
    isShowSecondaryMenu: false,
    isMenu:true,
    list:[{
      title:'推荐学校',
      href:'/home/recommend/company'
    },{
      title:'推荐精品',
      href:'/home/recommend/job'
    }]
  },
  {
    id:2,
    title:'分\u00A0\u00A0类',
    isShowSecondaryMenu: false,
    isMenu:true,
    list: [{
      title:'学校大全',
      href: '/home/class/company'
    },{
      title:'专业大全',
      href: '/home/class/job',
      subHerf:[
        '/home/class/job/addMajor'
      ]
    }]
  },
  {
    id:3,
    title:'就\u00A0\u00A0业',
    isShowSecondaryMenu: false,
    isMenu:false,
    list: [{
      href: '/home/employment'
    }]
  },
  {
    id:4,
    title:'校\u00A0&\u00A0企',
    isShowSecondaryMenu: false,
    isMenu:true,
    list: [{
      title: '学校',
      href: '/home/adv/school'
    },{
      title: '企业',
      href: '/home/adv/company'
    },{
      title: '资本',
      href: '/home/adv/capital'
    }]
  },
  {
    id:5,
    title:'横\u00A0\u00A0幅',
    isShowSecondaryMenu: false,
    isMenu:false,
    list: [{
      href: '/home/banner'
    }]
  },
  {
    id:6,
    title:'纷\u00A0\u00A0声',
    isShowSecondaryMenu: false,
    isMenu:false,
    list: [{
      href: '/home/fensheng'
    }]
  }
];

const RoleEnum = {
  1:{
    name:'超级管理员',
    value:1
  },
  2:{
    name:'普通管理员',
    value:2
  },
  3:{
    name:'普通用户',
    value:3
  },
  4:{
    name:'安装管理员',
    value:4
  }
};


class Home extends BaseComponent {
  constructor(){
    super(...arguments);
    let blocks = blocksManager;
    if(auth.getUser().Type===1){
      blocks = blocksSuper;
    }
    this.state = {
      isModel:false,
      userInfo:auth.getUser(),
      isMini:false,
      isLoadedGroup: false,
      list:[],
      blocks: blocks,
      isShowEditModal: false,
      routerPath:'',
    }
  }
  componentDidMount(){
    if(window.innerWidth < 1300){
      this.setState({isMini:true})
    }
    this.setState({sidebarHeight:window.innerHeight});
  }


  handleShowSecondaryMenuById(id){
    let block = this.state.blocks;
    block.map((item,index)=>{
      if(item.id === id){
        item.isShowSecondaryMenu = !item.isShowSecondaryMenu;
      }
    });
    this.setState({blocks:block})
  }


  render(){
    let {userInfo,isHidden,isMini,list,blocks,sidebarHeight} = this.state;
    let path  = this.props.location.pathname;
    let num = 0;
    blocks = blocks.map((item,index)=>{
      item.isLink = item.list.some((i)=>{
          if(i.href === path){
            return true
          }else{
            return(i.subHerf && i.subHerf.some((subItem)=>{
              return subItem === path;
            }));
          }
      });
      if(item.isLink){
        num = index
      }
      return item
    });

    return (
      <div className={'wrapper'}>
        <NavBar userInfo={userInfo}
                handleClick={()=>{this.setState({isShowEditModal:true,isLoadedGroup:false,routerPath:path})}}/>
        <div className="content">

          <Affix >
          <SideBar height={sidebarHeight} path={path} blocks = {blocks} isMini={isMini} isHidden={isHidden} id={num}
                   userInfo={userInfo}
                   handleShow={(i)=>this.handleShowSecondaryMenuById(i)}/>
          </Affix>

          <MainBody>
            {this.props.children}
          </MainBody>
          <div style={{display:'none'}}>
            {
              list.map((i,n)=><div key={n}>{i.name}</div>)
            }
          </div>
        </div>
      </div>
    )
  }
}
module.exports = Home;
